import { BorderType } from '../../../constants/excel';
export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheet, selections, _oldValue, border } = options;
        const line = new GC.Spread.Sheets.LineBorder('#000', GC.Spread.Sheets.LineStyle.thin);
        const boldLine = new GC.Spread.Sheets.LineBorder('#000', GC.Spread.Sheets.LineStyle.medium);
        const noLine = new GC.Spread.Sheets.LineBorder('#000', GC.Spread.Sheets.LineStyle.empty);
        const getBorder = (border) => {
            if (border === BorderType.All) {
                return { border: line, options: { all: true } };
            } else if (border === BorderType.BoldLine) {
                return { border: boldLine, options: { outline: true } };
            } else if (border === BorderType.Bottom) {
                return { border: line, options: { bottom: true } };
            } else if (border === BorderType.Left) {
                return { border: line, options: { left: true } };
            } else if (border === BorderType.Right) {
                return { border: line, options: { right: true } };
            } else if (border === BorderType.Top) {
                return { border: line, options: { top: true } };
            } else if (border === BorderType.None) {
                return { border: noLine, options: { all: true } };
            } else if (border === BorderType.OutLine) {
                return { border: line, options: { outline: true } };
            }
            return undefined;
        }
        try {
            sheet.suspendPaint();
            if (isUndo) {
                selections.forEach(range => {
                    for (let row = range.row, i = 0; i < range.rowCount; i++ , row++) {
                        for (let col = range.col, j = 0; j < range.colCount; j++ , col++) {
                            const cell = sheet.getCell(row, col);
                            const b = _oldValue[row][col];
                            cell.borderBottom(b.borderBottom);
                            cell.borderTop(b.borderTop);
                            cell.borderLeft(b.borderLeft);
                            cell.borderRight(b.borderRight);
                        }
                    }
                });
            } else {
                selections.forEach(range => {
                    for (let row = range.row, i = 0; i < range.rowCount; i++ , row++) {
                        for (let col = range.col, j = 0; j < range.colCount; j++ , col++) {
                            const cell = sheet.getCell(row, col);
                            const oldValue = {
                                borderBottom: cell.borderBottom(),
                                borderLeft: cell.borderLeft(),
                                borderRight: cell.borderRight(),
                                borderTop: cell.borderTop()
                            };
                            if (!options._oldValue) {
                                options._oldValue = {};
                            }
                            if (!options._oldValue[row]) {
                                options._oldValue[row] = {};
                            }
                            options._oldValue[row][col] = oldValue;
                        }
                    }
                    const outCell = sheet.getRange(range.row, range.col, range.rowCount, range.colCount);
                    const lineParams = getBorder(border);
                    if (lineParams) {
                        outCell.setBorder(lineParams.border, lineParams.options);
                    }
                });
            }
            sheet.resumePaint();
        } catch (e) {
            if(!!(window.console&&window.console.log)) {
                window.console.log(e);
            }
        }
    }
};